#ifndef UDP_SOCKET_IO_H
#define UDP_SOCKET_IO_H

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/time.h>

/* Estrutura para enviar/receber numero de operacao,
   id e nota de um filme em um mesmo datagrama */
typedef struct Request{
  char opt; /* opcao escolhida */
  int id;   /* id do filme     */
  int nota; /* nota do filme */
}request;

/* Tempo de timeout */
#define TIMEOUT_SEC 1
#define TIMEOUT_USEC 0

/* Macros para tipo de pedido de operacao */
#define REQ_OPCAO 0
#define REQ_OPCAO_ID 1
#define REQ_OPCAO_ID_NOTA 2


/* Funcoes de leitura e escrita em sockets UDP */

int read_int(int sockfd, int *n, int flags, struct sockaddr *from, int *fromlen);
int read_string(int sockfd, char *str, int max, int flags, struct sockaddr *from, int *fromlen);
int read_request(int sockfd, request *req, int flags, struct sockaddr *from, int *fromlen);
int readn(int sockfd, void *buf, int n, int flags, struct sockaddr *from, int *fromlen);

int write_int(int sockfd, int n, struct sockaddr *to, socklen_t tolen);
int write_string(int sockfd, char *str, int max, struct sockaddr *to, socklen_t tolen);
int write_request(int sockfd, request *req, int args, struct sockaddr *to, socklen_t tolen);
int writen(int sockfd, void *buf, int n, struct sockaddr *to, socklen_t tolen);

#endif
